package booking.misc;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.xml.crypto.Data;

public class Booking {

	private int flightId;
	private int depatureairportId;
	private int arrivalairportId;
	private Timestamp tod;
	private double flightPrice;
	private String Depature_City;
	private String Arrival_City;
	private int user_ID;
	private int flight_ID;
	
	public Booking(int id, int depaturairport, int arrivalairport,
			Timestamp timeofdepatur, double price, String Depature_City, String Arrival_City) {
		flightId = id;
		depatureairportId = depaturairport;
		arrivalairportId = arrivalairport;
		tod = timeofdepatur;
		flightPrice = price;
		this.Depature_City = Depature_City;
		this.Arrival_City = Arrival_City;
		this.user_ID = user_ID;
		this.flight_ID = flight_ID;
	}

	public int getFlightId() {
		return flightId;
	}

	public int getDepatureairportId() {
		return depatureairportId;
	}

	public int getArrivalairportId() {
		return arrivalairportId;
	}

	public Timestamp getTod() {
		return tod;
	}

	public double getFlightPrice() {
		return flightPrice;
	}

	public void setFlightId(int flightId) {
		this.flightId = flightId;
	}

	public void setDepatureairportId(int depatureairportId) {
		this.depatureairportId = depatureairportId;
	}

	public void setArrivalairportId(int arrivalairportId) {
		this.arrivalairportId = arrivalairportId;
	}

	public void setTod(Timestamp tod) {
		this.tod = tod;
	}

	public void setFlightPrice(double flightPrice) {
		this.flightPrice = flightPrice;
	}

	public String getDepature_City() {
		return Depature_City;
	}

	public String getArrival_City() {
		return Arrival_City;
	}

	public void setDepature_City(String depature_City) {
		Depature_City = depature_City;
	}

	public void setArrival_City(String arrival_City) {
		Arrival_City = arrival_City;
	}

	public int getUser_ID() {
		return user_ID;
	}

	public int getFlight_ID() {
		return flight_ID;
	}

	public void setUser_ID(int user_ID) {
		this.user_ID = user_ID;
	}

	public void setFlight_ID(int flight_ID) {
		this.flight_ID = flight_ID;
	}

	public void setVisible(boolean b) {
		// TODO Auto-generated method stub
	}

	public boolean dbInsert() {
		String sql = "INSERT INTO bookings (user_ID, flight_ID) "
				+ "VALUES (?, ?)";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql,
					PreparedStatement.RETURN_GENERATED_KEYS);
			statement.setInt(1, user_ID);
			statement.setInt(2, flight_ID);			
			statement.executeUpdate();
			ResultSet keys = statement.getGeneratedKeys();
			if (keys.next()) {
				user_ID = keys.getInt(1);
			}
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}

//	public boolean dbUpdate() {
//		String sql = "UPDATE flights SET depatureairportId=?, arrivalportId=?, tod=?, price=? "
//				+ "WHERE ID=?";
//		try (Connection conn = DBConnectionData.getConnection()) {
//			PreparedStatement statement = conn.prepareStatement(sql);
//			statement.setInt(1, depatureairportId);
//			statement.setInt(2, arrivalairportId);
//			statement.setTimestamp(3, tod);
//			statement.setDouble(4, flightPrice);
//			statement.setInt(5, flightId);
//			statement.executeUpdate();
//			return true;
//		} catch (SQLException ex) {
//			ex.printStackTrace();
//			return false;
//		}
//	}

	public boolean dbDelete() {
		String sql = "DELETE FROM Bookings WHERE ID=?";
		try (Connection conn = DBConnectionData.getConnection()) {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, flight_ID);
			statement.executeUpdate();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}
}